import re
from typing import Optional
from utils.logger import get_logger

# 获取日志记录器
logger = get_logger(__name__)


class StaffNameValidationError(Exception):
    """员工姓名验证错误异常类"""
    pass


class WorkHoursQuery:
    staff_name: Optional[str] = None
    framework_name: Optional[str] = None
    project_name: Optional[str] = None
    start_date: Optional[str] = None
    end_date: Optional[str] = None

    def __init__(self, staff_name: Optional[str] = None, framework_name: Optional[str] = None, project_name: Optional[str] = None, start_date: Optional[str] = None, end_date: Optional[str] = None):
        self.staff_name = self._validate_staff_name(staff_name)
        self.framework_name = framework_name
        self.project_name = project_name
        self.start_date = start_date
        self.end_date = end_date

    def _validate_staff_name(self, staff_name: Optional[str]) -> Optional[str]:
        """
        验证员工姓名，只允许中文、英文和数字，不允许特殊符号
        
        Args:
            staff_name: 员工姓名
            
        Returns:
            验证通过的员工姓名
            
        Raises:
            StaffNameValidationError: 当员工姓名包含特殊符号时抛出
        """
        if staff_name is None:
            return None
        
        # 正则表达式：只允许中文、英文字母和数字
        # \u4e00-\u9fff 是中文字符范围
        # a-zA-Z 是英文字母
        # 0-9 是数字
        pattern = r'^[\u4e00-\u9fff a-zA-Z0-9]+$'
        
        if not re.match(pattern, staff_name):
            error_msg = f"🚫 员工姓名验证失败：'{staff_name}' 包含特殊符号。只允许中文、英文和数字。"
            logger.error(error_msg)
            raise StaffNameValidationError(error_msg)
        
        return staff_name

    def to_dict(self):
        return {
            "staff_name": self.staff_name,
            "framework_name": self.framework_name,
            "project_name": self.project_name,
            "start_date": self.start_date,
            "end_date": self.end_date
        }
